package fuentes;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;


public class Fuente {
	private File inFile;
	private BufferedReader reader;
	long nro_linea;
	int pos_linea;
	String line;
	String path;
	
	public Fuente(String path){
		this.path=path;
		inFile = new File(path);
		try {
			reader = new BufferedReader(new FileReader(inFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			line=reader.readLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		pos_linea=0;
		nro_linea=1;
	}
	public void close(){
		try {
			reader.close( );
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public String getCaracter(){
		if (line==null){
			close();
			return "/e";
			
		}
		if (pos_linea == line.length()){
			pos_linea = 0;
			nro_linea++;
			try {
				line=reader.readLine();
			} catch (IOException e) {
				e.printStackTrace();
			}
			return (new String("/n"));
		}
		String ret="";
		ret+=line.charAt(pos_linea);
		pos_linea++;
		return ret;
	}
	
	public void retCaracter(){
	   if (line!=null)
		if (pos_linea!=0)
			pos_linea--;
	}
	public long getLinea(){
		return nro_linea;
	}
	public boolean isEnd(){
		return (line==null);
	}
	
	public static String getFuente(String path){
		String codigo = "";
		File inFile = new File(path);
		BufferedReader reader=null;
		try {
			 reader = new BufferedReader(new FileReader(inFile));
		} catch (FileNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		try {
			String linea=reader.readLine();
			
			while (linea!=null){
				codigo+=linea + "\n";
				linea=reader.readLine();
			}
			//codigo=codigo.substring(0,codigo.length()-1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return codigo;
	}
	public static String reciclarFuente(String fuente, String path){
		FileWriter fichero = null;
        PrintWriter pw = null;
        try
        {
            fichero = new FileWriter(path);
            pw = new PrintWriter(fichero);
            String linea="";
            for (int i = 0; i < fuente.length(); i++){
            	if((fuente.charAt(i)=='\n')||(i==(fuente.length()-1))){
            		if (fuente.charAt(i)!='\n')
            			linea+=fuente.charAt(i);
            		pw.println(linea);
            		linea="";
            	}
            	else
            		linea+=fuente.charAt(i);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
           try {
           // Nuevamente aprovechamos el finally para 
           // asegurarnos que se cierra el fichero.
           if (null != fichero)
              fichero.close();
           } catch (Exception e2) {
              e2.printStackTrace();
           }
        }
		
		
		
		return path;
	
	}
}
